package com.kakarote.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.kakarote.core.servlet.BaseServiceImpl;
import com.kakarote.finance.common.AccountSet;
import com.kakarote.finance.entity.BO.FinanceSearchCertificateBO;
import com.kakarote.finance.entity.PO.FinanceCertificateDetail;
import com.kakarote.finance.mapper.FinanceCertificateDetailMapper;
import com.kakarote.finance.service.IFinanceCertificateDetailService;
import com.kakarote.finance.service.IFinanceSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 凭证详情 服务实现类
 * </p>
 *
 * @author zhangzhiwei
 * @since 2021-08-25
 */
@Service
public class FinanceCertificateDetailServiceImpl extends BaseServiceImpl<FinanceCertificateDetailMapper, FinanceCertificateDetail> implements IFinanceCertificateDetailService {

    @Autowired
    private IFinanceSubjectService financeSubjectService;

    @Override
    public List<Long> queryIdsByCondition(FinanceSearchCertificateBO searchCertificateBO) {
        if (searchCertificateBO.getSubjectId() != null) {
            List<Long> subjectIds = financeSubjectService.queryIds(searchCertificateBO.getSubjectId(), null, null);
            searchCertificateBO.setSubjectIds(subjectIds);
        }
        searchCertificateBO.setAccountId(AccountSet.getAccountSetId());
        return getBaseMapper().queryIdsByCondition(searchCertificateBO);
    }

    @Override
    public List<FinanceCertificateDetail> queryBcjd(String beginTime, String endTime,List<Long> listSubjectIds) {
        if(listSubjectIds.size()<=0){
            return new ArrayList<>();
        }
        QueryWrapper<FinanceCertificateDetail> tWrapper = new QueryWrapper<>();
        tWrapper.select("subject_id,  \n" +
//                "    IFNULL(price,0) as price," +
                "    IFNULL(SUM(quantity),0) AS quantity,  \n" +
                "    IFNULL(SUM(debtor_balance),0) AS debtor_balance,  \n" +
                "    IFNULL(SUM(credit_balance),0) AS credit_balance  ");
        tWrapper.eq("account_id", AccountSet.getAccountSetId());
        tWrapper.apply(StringUtils.isNotEmpty(beginTime),"create_time >= STR_TO_DATE('" + beginTime + "01', '%Y%m%d')");
        tWrapper.apply(StringUtils.isNotEmpty(endTime),"create_time <= LAST_DAY('" + endTime + "01')");
        tWrapper.in("subject_id",listSubjectIds);
        tWrapper.groupBy("subject_id,debtor_balance,credit_balance");
        List<FinanceCertificateDetail> financeCertificateDetails = baseMapper.selectList(tWrapper);
        return financeCertificateDetails;
    }

    @Override
    public List<FinanceCertificateDetail> querySumJdBySubjectId(String beginTime, String endTime,List<Long> listSubjectIds) {
        if(listSubjectIds.size()<=0){
            return new ArrayList<>();
        }
        QueryWrapper<FinanceCertificateDetail> tWrapper = new QueryWrapper<>();
        tWrapper.select("subject_id,  \n" +
//                "    IFNULL(price,0) as price," +
                "    IFNULL(SUM(quantity),0) AS quantity,  \n" +
                "    IFNULL(SUM(debtor_balance),0) AS debtor_balance,  \n" +
                "    IFNULL(SUM(credit_balance),0) AS credit_balance  ");
        tWrapper.eq("account_id", AccountSet.getAccountSetId());
        tWrapper.apply(StringUtils.isNotEmpty(beginTime),"create_time >= STR_TO_DATE('" + beginTime + "01', '%Y%m%d')");
        tWrapper.apply(StringUtils.isNotEmpty(endTime),"create_time <= LAST_DAY('" + endTime + "01')");
        tWrapper.in("subject_id",listSubjectIds);
        tWrapper.groupBy("subject_id");
        List<FinanceCertificateDetail> financeCertificateDetails = baseMapper.selectList(tWrapper);
        return financeCertificateDetails;
    }
}
